package com.ts.main.mini.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ts.main.mini.context.UserContext;
import com.ts.main.mini.dto.AddressDto;
import com.ts.main.mini.dto.AddressPageSearchDto;
import com.ts.main.mini.entity.UserAddressEntity;
import com.ts.main.mini.exception.RPage;
import com.ts.main.mini.mapper.UserAddressMapper;
import com.ts.main.mini.vo.UserAddressVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lk
 * @description 针对表【t_user_address(用户收货地址表)】的数据库操作Service实现
 * @createDate 2025-05-16 15:12:07
 */
@AllArgsConstructor
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddressEntity> {

    public RPage<List<UserAddressVo>> getAddressPage(AddressPageSearchDto dto) {
        Integer userId = UserContext.getUser().getUserId();
        dto.setUserId(userId);
        final Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<UserAddressVo> list = baseMapper.getAddressPage(dto.getUserId());
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public List<UserAddressVo> getAddressList() {
        Integer userId = UserContext.getUser().getUserId();
        return baseMapper.getAddressPage(userId);
    }

    public UserAddressVo getAddressDetails(Integer addressId) {
        return BeanUtil.copyProperties(getById(addressId), UserAddressVo.class);
    }

    @Transactional
    public Boolean saveAddress(AddressDto dto) {
        Integer userId = UserContext.getUser().getUserId();
        dto.setUserId(userId);
        if (!ObjectUtil.isEmpty(dto.getIsDefault()) && dto.getIsDefault()) {
            baseMapper.updateAddressIsDefault(dto.getUserId(), null);
        }
        UserAddressEntity userAddressEntity = new UserAddressEntity();
        BeanUtil.copyProperties(dto, userAddressEntity);
        return save(userAddressEntity);
    }

    @Transactional
    public Boolean updateAddress(AddressDto dto) {
        Integer userId = UserContext.getUser().getUserId();
        dto.setUserId(userId);
        if (!ObjectUtil.isEmpty(dto.getIsDefault()) && dto.getIsDefault()) {
            baseMapper.updateAddressIsDefault(dto.getUserId(), dto.getUserAddressId());
        }
        UserAddressEntity userAddressEntity = new UserAddressEntity();
        BeanUtil.copyProperties(dto, userAddressEntity);
        return updateById(userAddressEntity);
    }

    public Boolean deleteAddress(List<Integer> ids) {
        LambdaUpdateWrapper<UserAddressEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(UserAddressEntity::getIsDeleted, true)
                .in(UserAddressEntity::getUserAddressId, ids);
        return update(updateWrapper);
    }
}




